package cn.zc.curriculum.service.service.impl;

import cn.tools.core.exception.BizException;
import cn.tools.dozer.config.DozerUtils;
import cn.zc.curriculum.entity.CurriculumCourseware;
import cn.zc.curriculum.entity.Dto.CurriculumCoursewareDto;
import cn.zc.curriculum.service.mapper.CurriculumCoursewareMapper;
import cn.zc.curriculum.service.service.CurriculumCoursewareService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程课件 服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-17
 */
@Service
public class CurriculumCoursewareServiceImpl extends ServiceImpl<CurriculumCoursewareMapper, CurriculumCourseware> implements CurriculumCoursewareService {

    @Resource
    private DozerUtils dozerUtils;

    @Resource
    private CurriculumCoursewareMapper curriculumCoursewareMapper;

    @Override
    public void createCurriculumCourseware(CurriculumCoursewareDto curriculumCoursewareDto) {
        CurriculumCourseware courseware = dozerUtils.map(curriculumCoursewareDto, CurriculumCourseware.class);
        if(!save(courseware)){
            throw new BizException("创建课件失败");
        }
    }

    @Override
    public List<CurriculumCoursewareDto> findCurriculumCoursewareByMenu(String menuId) {
        QueryWrapper<CurriculumCourseware> wrapper = new QueryWrapper<>();
        wrapper.lambda();

        /**
         * 直接构建条件表达式，创建LambdaQueryChainWrapper，传入对应mapper，设定条件如我这里设定的等于，然后调用list表示查询后封装列表，如果只有一条数据可以直接.one结尾
         */
        List<CurriculumCourseware> courseware = new LambdaQueryChainWrapper<CurriculumCourseware>(curriculumCoursewareMapper)
                .eq(CurriculumCourseware::getMenuId,menuId)
                .list();

        List<CurriculumCoursewareDto> coursewareDtos = dozerUtils.mapList(courseware, CurriculumCoursewareDto.class);
        return coursewareDtos;
    }



    @Override
    public void updateCurriculumCoursewareByMenu(CurriculumCoursewareDto curriculumCoursewareDto) {
        CurriculumCourseware coursrware = dozerUtils.map(curriculumCoursewareDto, CurriculumCourseware.class);
        if(!updateById(coursrware)){
            throw new BizException("修改课件失败");
        }
    }

    @Override
    public void deleteCurriculumCoursewareByMenu(String id) {
        if(!removeById(id)){
            throw new BizException("删除课件失败");
        }
    }
}
